package com.springboot.frame.web.util;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * Cookie工具类
 * <pre>
 * Cookie的常用操作
 * </pre>
 *
 * @author madman
 */
public final class CookieUtils {

    public static final int COOKIE_MAX_AGE = 7 * 24 * 3600;

    public static final int COOKIE_HALF_HOUR = 30 * 60;

    private CookieUtils() {
    }

    /**
     * 在request对象中获取指定Cookie
     *
     * @param request    request对象
     * @param cookieName Cookie名称
     * @return 查找到的Cookie对象
     */
    public static Cookie getCookie(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie != null && cookieName.equals(cookie.getName())) {
                    return cookie;
                }
            }
        }
        return null;
    }

    /**
     * 删除Cookie
     *
     * @param request  request对象
     * @param response response对象
     * @param name     Cookie名称
     */
    public static void delCookie(HttpServletRequest request, HttpServletResponse response, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie != null && (name).equals(cookie.getName())) {
                    addCookie(response, name, null, null, null, 0);
                    return;
                }
            }
        }
    }

    /**
     * 修改cookie的value值
     *
     * @param request  request对象
     * @param response response对象
     * @param name     Cookie名称
     * @param value    Cookie值
     */
    public static void updateCookie(HttpServletRequest request, HttpServletResponse response, String name,
                                    String value) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie != null && (name).equals(cookie.getName())) {
                    addCookie(response, name, value, cookie.getPath(), cookie.getDomain(), cookie.getMaxAge());
                    return;
                }
            }
        }
    }

    /**
     * 添加cookie
     *
     * @param response response
     * @param name     Cookie名称
     * @param value    Cookie值
     * @param path     Cookie写入路径
     * @param domain   Cookie的作用域
     * @param timeout  Cookie有效期 (秒)
     */
    public static void addCookie(HttpServletResponse response, String name, String value, String path, String domain, int timeout) {
        Cookie cookie = new Cookie(name, value);
        if (!StringUtils.isBlank(path)) {
            path = "/";
        }
        if (!StringUtils.isBlank(domain)) {
            cookie.setDomain(domain);
        }
        cookie.setPath(path);
        cookie.setMaxAge(timeout);
        response.addCookie(cookie);
    }
    /**
     * 添加cookie,Https下使用 FIXME 未经过加密，不安全。待修复，选择适合的加密算法
     *
     * @param response 响应
     * @param name     名称
     * @param value    值
     * @param maxAge   存活时间
     */
    public static void addCookie(HttpServletResponse response, String name, String value,
                                 int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        cookie.setMaxAge(Math.abs(maxAge));
        response.addCookie(cookie);
    }

    /**
     * 添加cookie，Https下使用
     *
     * @param response 响应
     * @param name     名称
     * @param value    值
     * @param maxAge   存活时间
     */
    public static void addSSLCookie(HttpServletResponse response, String name, String value,
                                    int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        cookie.setSecure(true);
        if (maxAge > 0) {
            cookie.setMaxAge(maxAge);
        }
        response.addCookie(cookie);
    }


    /**
     * 移除cookie
     *
     * @param name 这个是名称，不是值
     */
    public static void removeCookie(HttpServletRequest request, HttpServletResponse response,
                                    String name) {
        if (null == name) {
            return;
        }
        Cookie cookie = getCookie(request, name);
        if (null != cookie) {
            addCookie(response, name, "", 0);
        }
    }

}
